package com.example.javamaday.leetcode;

public class LeetCodeTest0566 {

    public static void main(String[] args) {

        new LeetCodeTest0566().matrixReshape(new int[][]{{1, 2, 3, 4}},2, 2);
    }

    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int x = mat.length;
        int y = mat[0].length;
        // 校验是否可重塑
        if (x * y != r * c) {
            return mat;
        }
        int txIndex = 0;
        int tyIndex = 0;

        int[][] target = new int[r][c];
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                System.out.println(mat[i][j]);
                target[txIndex][tyIndex] = mat[i][j];
                tyIndex++;
                if (tyIndex == c) {
                    txIndex++;
                    tyIndex=0;
                }
            }
        }
        return target;
    }
}
